<template>
	<view v-if="isLogin">
		<home-module v-if="currentTab == 0" />
		<thing-module ref="thingRef" v-if="currentTab == 1" />
		<setting-module v-if="currentTab == 2" />

		<custom-tabbar />
	</view>
</template>

<script lang="ts" setup>
import HomeModule from './modules/home.vue';
import ThingModule from './modules/thing.vue';
import SettingModule from './modules/setting.vue';
import useAppStore from '@/store/app';
import { computed, ref } from 'vue';
import { onPullDownRefresh, onReachBottom } from '@dcloudio/uni-app';

const appStore = useAppStore();
const currentTab = computed(() => appStore.currentTab);

var thingRef = ref<InstanceType<typeof ThingModule>>(null);
var isLogin = computed(() => {
	return appStore.access_token ? true : false;
});

// 刷新
onPullDownRefresh(async () => {
	if (currentTab.value == 1) await thingRef.value._refresh();
	uni.stopPullDownRefresh();
});

// 下一页
onReachBottom(() => {
	if (currentTab.value == 1) thingRef.value._nextPage();
});

// 未登录提示
function handLogin() {
	uni.showModal({
		title: '系统提示',
		content: '你还没有登录呢！',
		showCancel: false,
		success: () => {
			// 跳转登录
			uni.navigateTo({ url: '/pages/login/login' });
		}
	});
}

if (!isLogin.value) handLogin();
</script>
